package com.serv;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.serv.config.datasource.dynamic.DynamicDataSourceRegister;
import com.serv.config.yaml.YamlFileApplicationContextInitializer;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {
        org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
        org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration.class,
        //org.activiti.spring.boot.RestApiAutoConfiguration.class,
        org.activiti.spring.boot.SecurityAutoConfiguration.class})
//@EnableConfigurationProperties可以在类外指定对应的配置文件进行加载.
//被加载的class需要配置ConfigurationProperties的注解来标识它是一个配置文件的解析类
//@EnableConfigurationProperties({BootDataSourceConfig.class,ActivitiDataSourceConfig.class})
//@PropertySource({"classpath:ds/datasource.yml"}) //propertySource可以加载yml,但不支持yml的解析
@Import({DynamicDataSourceRegister.class,YamlFileApplicationContextInitializer.class})
//@MapperScan("com.serv.model")
@ComponentScan(basePackages = {
        "com.serv",
        "org.activiti.explorer",
        })
@ServletComponentScan(basePackages = { "com.serv",})
public class Application extends SpringBootServletInitializer {
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setMaxFileSize("5MB");
		factory.setMaxRequestSize("5MB");
		return factory.createMultipartConfig();
	}

	@Primary
    @Bean
    public TaskExecutor primaryTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // add necessary properties to the executor
        return executor;
    }

//    @Bean
//    public EmbeddedServletContainerFactory servletContainer() {
//        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
//        factory.setPort(9000);
//        factory.setSessionTimeout(10, TimeUnit.MINUTES);
//        factory.addErrorPages(
//                new ErrorPage(HttpStatus.NOT_FOUND,
//                "/notfound.html"));
//        return factory;
//    }

	public static void main(String[] args) {
		SpringApplication.run(Application.class);
	}

}
